/**
 * Project Name:jiuselu-parent
 * File Name:UserAddressController.java
 * Package Name:com.fxp.project.jiuselu.web.business.v1.controller.user
 * Date:2021年11月10日 13:34
 * Copyright (c) 2021, Kai.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.web.business.v1.controller.user;

import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.base.request.ReqBase;
import com.fxp.project.jiuselu.web.business.v1.request.address.ReqUserAddress;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.user.entity.JslUserAddress;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName:UserAddressController<br/>
 * Function: 用户地址管理<br/>
 * Date:2021年11月10日 13:34<br/>
 *
 * @author Kai.Wang
 * @version 1.0.0
 * @see
 * @since JDK 8
 */
@RestController
@RequestMapping("/v1/user/address")
public class UserAddressController extends BaseController {

    @Autowired
    IJslUserAddressService jslUserAddressService;

    /**
     * list: 获取当前用户的地址列表<br/>
     *
     * @param reqBase
     * @param result
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 13:36
     */
    @PostMapping("list")
    @CrossOrigin
    public CommonResult<List<JslUserAddress>> list(@RequestBody @Validated ReqBase reqBase, BindingResult result) {
        super.checkBindResult(result);
        List<JslUserAddress> list =
            jslUserAddressService.selectJslUserAddressListByUserId(super.getCurrentUserInfo().getUserId());
        return CommonResult.success(list);
    }

    /**
     * defaultAddress: 获取当前用户配置的默认地址<br/>
     *
     * @param reqBase
     * @param result
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 14:22
     */
    @PostMapping("default")
    @CrossOrigin
    public CommonResult<JslUserAddress> defaultAddress(@RequestBody @Validated ReqBase reqBase, BindingResult result) {
        super.checkBindResult(result);
        JslUserAddress jslUserAddress = jslUserAddressService.selectDefaultJslUserAddressByUserIdWithAddressId(
            super.getCurrentUserInfo().getUserId());
        return CommonResult.success(jslUserAddress);
    }

    /**
     * detail: 获取当前用户信息<br/>
     *
     * @param addressId
     * @param reqBase
     * @param result
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 13:42
     */
    @PostMapping("detail/{addressId}")
    @CrossOrigin
    public CommonResult<JslUserAddress> detail(@PathVariable("addressId") Long addressId,
        @RequestBody @Validated ReqBase reqBase, BindingResult result) {
        super.checkBindResult(result);
        JslUserAddress jslUserAddress =
            jslUserAddressService.selectJslUserAddressByUserIdWithAddressId(super.getCurrentUserInfo().getUserId(),
                addressId);
        return CommonResult.success(jslUserAddress);
    }

    /**
     * saveOrUpdate: 保存或者更新当前用户地址<br/>
     *
     * @param reqUserAddress
     * @param result
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 14:31
     */
    @PostMapping("save")
    @CrossOrigin
    public CommonResult saveOrUpdate(@RequestBody @Validated ReqUserAddress reqUserAddress, BindingResult result) {
        super.checkBindResult(result);
        JslUserAddress jslUserAddress = new JslUserAddress();
        BeanUtils.copyProperties(reqUserAddress, jslUserAddress);
        jslUserAddress.setUserId(super.getCurrentUserInfo().getUserId());
        if (null != jslUserAddress.getAddressId()) {
            jslUserAddress.setUpdateBy(super.getCurrentUserInfo().getNickName());
            jslUserAddress.setUpdateTime(LocalDateTime.now());
            JslUserAddress defaultAddress = jslUserAddressService.selectDefaultJslUserAddressByUserIdWithAddressId(
                super.getCurrentUserInfo().getUserId());
            // 当前没有配置默认地址新增加的地址为默认地址
            if (null == defaultAddress) {
                jslUserAddress.setIsDefault(1);
            }
        } else {
            jslUserAddress.setCreateBy(super.getCurrentUserInfo().getNickName());
            jslUserAddress.setCreateTime(LocalDateTime.now());
        }
        // FIXME 简单粗暴修改默认地址重复的的问题。
        jslUserAddressService.saveOrUpdate(jslUserAddress);
        if (reqUserAddress.getPhone().length()>11){
            CommonResult.failed("手机号有问题");
        }
        if (null != jslUserAddress.getIsDefault() && jslUserAddress.getIsDefault() == 1) {
            return CommonResult.success(jslUserAddressService.updateUserAddressToDefault(jslUserAddress.getAddressId(),
                super.getCurrentUserInfo().getUserId()));
        }
        return CommonResult.success(1);
    }

    /**
     * setDefaultAddress: 设置当前地址为默认地址，有且只有一个<br/>
     *
     * @param addressId
     * @param reqBase
     * @param result
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 15:18
     */
    @PostMapping("default/{addressId}")
    @CrossOrigin
    public CommonResult setDefaultAddress(@PathVariable("addressId") Long addressId,
        @RequestBody @Validated ReqBase reqBase, BindingResult result) {
        super.checkBindResult(result);
        return CommonResult.success(
            jslUserAddressService.updateUserAddressToDefault(addressId, super.getCurrentUserInfo().getUserId()));
    }

    /**
     * delete: 删除当前用户的地址<br/>
     *
     * @param addressId
     * @param reqBase
     * @param result
     * @return
     * @author Kai.Wang
     * @date 2021/11/10 14:34
     */
    @PostMapping("delete/{addressId}")
    @CrossOrigin
    public CommonResult delete(@PathVariable("addressId") Long addressId, @RequestBody @Validated ReqBase reqBase,
        BindingResult result) {
        super.checkBindResult(result);
        JslUserAddress param = new JslUserAddress();
        param.setUserId(super.getCurrentUserInfo().getUserId());
        param.setAddressId(addressId);
        return CommonResult.success(jslUserAddressService.removeById(param));
    }
}
